<?php
/**
 * @version		$Id: manager.php 10381 2008-06-01 03:35:53Z yopensource $
 * @package		YOS News Crawler
 * @subpackage	Link Manager
 * @license		commercial
 * @author 		yopensource
 * @email		yopensource@gmail.com
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.model');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');

// Set the table directory
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

/**
 * Weblinks Component Weblink Model
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class YOS_NewsModelCmsManager extends JModel
{
	/**
	 * amMap data array
	 *
	 * @var array
	 */
	var $_data = null;	
	/**
	 * job
	 *
	 * @var array
	 */
	var $_job	=	null;

	/**
	 * Constructor
	 *
	 * @since 0.9
	 */
	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;
	}

	function getData(){	
		// Lets load the content if it doesn't already exist
		$arrFolder=JFolder::listFolderTree(JPATH_COMPONENT.DS.'cms','',1);
		$data=array();
		for ($i=0;$i<count($arrFolder);$i++)
		{
			$cms=$arrFolder[$i]['name'];
			
			$path=JPATH_COMPONENT.DS.'cms'.DS.$cms.DS.'cms_config.xml';	
			$arrAttribute=$this->getFieldName($path);
			
			$file=JPATH_COMPONENT.DS.'cms'.DS.$cms.DS.'cms_config.php';
			if (!JFile::exists($file)) {						 
				$str_file_content = "
				<?php
				// no direct access
				defined('_JEXEC') or die('Restricted access');	";
				foreach ($arrAttribute as $field_name) {					
					$str_file_content.="
				$$field_name=\"\"; ";
				}
			JFile::write($file, $str_file_content);	
			}			
			
			$item_params    = new JParameter("",JPATH_COMPONENT.DS.'cms'.DS.$cms.DS.'cms_config.xml');
			
			$params=$item_params->getParams();			
			//JParameter::			
			require($file);
			foreach ($arrAttribute as $field_name) {					
				$item_params->set($field_name,$$field_name);
			}			
			$obj=new stdClass();
			$obj->name=$cms;
			$obj->form=$item_params->render($cms);
			$data[]=$obj;
		}		
		return $data;
	}
	function save()
	{
		$arrFolder=JFolder::listFolderTree(JPATH_COMPONENT.DS.'cms','',1);
		for ($i=0;$i<count($arrFolder);$i++)
		{
			$cms=$arrFolder[$i]['name'];
			$params=JRequest::getVar($cms,null,'','array');
			$file=JPATH_COMPONENT.DS.'cms'.DS.$cms.DS.'cms_config.php';
			
			$path=JPATH_COMPONENT.DS.'cms'.DS.$cms.DS.'cms_config.xml';	
			$arrAttribute=$this->getFieldName($path);
			$str_file_content = "
				<?php
				// no direct access
				defined('_JEXEC') or die('Restricted access');	";
				
			foreach ($arrAttribute as $field_name) {					
					$str_file_content.="
				$$field_name=\"$params[$field_name]\"; ";
				}		
			JFile::write($file, $str_file_content);			
		}	
	}
	function getFieldName($path)
	{
		$arrAttribute=array();		
		$fileXML=$path;	
		$xml = & JFactory::getXMLParser('Simple');
			$xml->loadFile($fileXML);
		$nodes=$xml->document->params[0]->param;
		foreach($nodes as $node) {
		 	$arrAttribute[]=$node->attributes('name');
		 }
		 return $arrAttribute;
	}	
}